//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "GaussianQuadrature.h"
using namespace Cephei::QL::Math::Integrals;
#include <gen/QL/Math/Integrals/GaussianOrthogonalPolynomial.h>
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Math::Integrals::CGaussianQuadrature::CGaussianQuadrature (UInt64 n, Cephei::QL::Math::Integrals::IGaussianOrthogonalPolynomial^ p) 
{
	_pSpinlock = new boost::detail::spinlock ();
    CGaussianOrthogonalPolynomial^ _Cp;
    try
    {
#ifdef HANDLE
        _phGaussianQuadrature = NULL;
#endif
        QuantLib::Size _n = (QuantLib::Size)ValueHelper::Convert (n);
        _Cp = safe_cast<CGaussianOrthogonalPolynomial^> (p);
        _Cp->Lock();
        QuantLib::GaussianOrthogonalPolynomial& _p = static_cast<QuantLib::GaussianOrthogonalPolynomial&> (_Cp->GetReference ()); 
        _ppGaussianQuadrature = new boost::shared_ptr<QuantLib::GaussianQuadrature> (new QuantLib::GaussianQuadrature ( _n,  _p ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cp != nullptr) _Cp->Unlock();
    }
}
Cephei::QL::Math::Integrals::CGaussianQuadrature::CGaussianQuadrature (boost::shared_ptr<QuantLib::GaussianQuadrature>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phGaussianQuadrature = NULL;
#endif
	_ppGaussianQuadrature = &childNative;
    
}
Cephei::QL::Math::Integrals::CGaussianQuadrature::CGaussianQuadrature (QuantLib::GaussianQuadrature& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phGaussianQuadrature = NULL;
#endif
	_ppGaussianQuadrature = new boost::shared_ptr<QuantLib::GaussianQuadrature> (&childNative);
    
    _GaussianQuadratureOwner = owner;
    
}

Cephei::QL::Math::Integrals::CGaussianQuadrature::CGaussianQuadrature (CGaussianQuadrature^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phGaussianQuadrature = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppGaussianQuadrature = new boost::shared_ptr<QuantLib::GaussianQuadrature> (copy->GetShared());
        
    }
}
Cephei::QL::Math::Integrals::CGaussianQuadrature::CGaussianQuadrature (System::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phGaussianQuadrature = NULL;
#endif
	if (!t->IsSubclassOf(CGaussianQuadrature::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Math::Integrals::CGaussianQuadrature::CGaussianQuadrature (QuantLib::Handle<QuantLib::GaussianQuadrature>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phGaussianQuadrature = &childNative;
	_ppGaussianQuadrature = &static_cast<boost::shared_ptr<QuantLib::GaussianQuadrature>>(childNative.currentLink());
    
    _GaussianQuadratureOwner = owner;
}
Cephei::QL::Math::Integrals::CGaussianQuadrature::CGaussianQuadrature (QuantLib::Handle<QuantLib::GaussianQuadrature> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phGaussianQuadrature = &childNative;
	_ppGaussianQuadrature = &static_cast<boost::shared_ptr<QuantLib::GaussianQuadrature>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Math::Integrals::CGaussianQuadrature::CGaussianQuadrature (QuantLib::GaussianQuadrature childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phGaussianQuadrature = NULL;
#endif
	_ppGaussianQuadrature = new boost::shared_ptr<QuantLib::GaussianQuadrature> (new QuantLib::GaussianQuadrature (childNative));
    
}
#endif

Cephei::QL::Math::Integrals::CGaussianQuadrature::~CGaussianQuadrature ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppGaussianQuadrature != NULL)
    {
	    delete _ppGaussianQuadrature;
        _ppGaussianQuadrature = NULL;
    }
}
Cephei::QL::Math::Integrals::CGaussianQuadrature::!CGaussianQuadrature ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppGaussianQuadrature != NULL)
    {
	    delete _ppGaussianQuadrature;
    }
}
QuantLib::GaussianQuadrature& Cephei::QL::Math::Integrals::CGaussianQuadrature::GetReference ()
{
    if (_ppGaussianQuadrature == NULL) throw gcnew NativeNullException ();
	return **_ppGaussianQuadrature;
}
boost::shared_ptr<QuantLib::GaussianQuadrature>& Cephei::QL::Math::Integrals::CGaussianQuadrature::GetShared ()
{
    if (_ppGaussianQuadrature == NULL) throw gcnew NativeNullException ();
	return *_ppGaussianQuadrature;
}
QuantLib::GaussianQuadrature* Cephei::QL::Math::Integrals::CGaussianQuadrature::GetPointer ()
{
    if (_ppGaussianQuadrature == NULL) throw gcnew NativeNullException ();
	return &**_ppGaussianQuadrature;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::GaussianQuadrature>& Cephei::QL::Math::Integrals::CGaussianQuadrature::GetHandle ()
{
	if (_phGaussianQuadrature == NULL)
	{
		_phGaussianQuadrature = new Handle<QuantLib::GaussianQuadrature> (*_ppGaussianQuadrature);
	}
	return *_phGaussianQuadrature;
}
#endif
bool Cephei::QL::Math::Integrals::CGaussianQuadrature::HasNative () 
{
	return (_ppGaussianQuadrature != NULL);
}

UInt64 Cephei::QL::Math::Integrals::CGaussianQuadrature::Order::get ()
{
    try
    {
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppGaussianQuadrature)->order ( );   
        UInt64 _nrv = (UInt64)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Math::Integrals::IGaussianQuadrature^ Cephei::QL::Math::Integrals::CGaussianQuadrature_Factory::Create (UInt64 n, Cephei::QL::Math::Integrals::IGaussianOrthogonalPolynomial^ p)
{
    return gcnew CGaussianQuadrature ( n,  p);
}
